#include "Rect.h"

#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>

#include <math.h>

static const double Pi = 3.14159265358979323846264338327950288419717;
static double TwoPi = 2.0 * Pi;

static qreal normalizeAngle(qreal angle)
{
	while (angle < 0)
		angle += TwoPi;
	while (angle > TwoPi)
		angle -= TwoPi;
	return angle;
}

Rect::Rect()
	: angle(0), speed(1),
	  color(qrand() % 256, qrand() % 256, qrand() % 256)
{
}

QRectF Rect::boundingRect() const
{
	qreal adjust = 0.5;
	return QRectF(-18 - adjust, -22 - adjust,
				  36 + adjust, 60 + adjust);
}

QPainterPath Rect::shape() const
{
	QPainterPath path;
	path.addRect(-10, -20, 20, 40);
	return path;
}

void Rect::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
	// Body
	painter->setBrush(color);
	painter->drawRect(-10, -20, 20, 40);
}

void Rect::advance(int step)
{
	if (!step)
		return;

	QLineF lineToCenter(QPointF(0, 0), mapFromScene(QCursor::pos()));
	if (lineToCenter.length() > 150) {
		qreal angleToCenter = ::acos(lineToCenter.dx() / lineToCenter.length());
		if (lineToCenter.dy() < 0)
			angleToCenter = TwoPi - angleToCenter;
		angleToCenter = normalizeAngle((Pi - angleToCenter) + Pi / 2);

		if (angleToCenter < Pi && angleToCenter > Pi / 4) {
			// Rotate left
			angle += (angle < -Pi / 2) ? 0.25 : -0.25;
		} else if (angleToCenter >= Pi && angleToCenter < (Pi + Pi / 2 + Pi / 4)) {
			// Rotate right
			angle += (angle < Pi / 2) ? 0.25 : -0.25;
		}
	} else if (::sin(angle) < 0) {
		angle += 0.25;
	} else if (::sin(angle) > 0) {
		angle -= 0.25;
	}

	//speed += (-50 + qrand() % 100) / 100.0;

	qreal dx = ::sin(angle) * 10;

	setRotation(rotation() + dx);
	setPos(mapToParent(0, -(3 + sin(speed) * 3)));
}
